<template>
	<section>
		<!--高级查询 +工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form  :inline="true">
				<el-form-item>
					<el-input v-model="keyword" placeholder="关键字" ></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="warning" v-on:click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="addHandler">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

    <!--列表v-loading="listLoading"-->
    <el-table border :data="tableDatas" v-loading="listLoading" @selection-change="handleSelectionChange"
              highlight-current-row :row-key="getKey" ref="multable" style="width: 100%;">
      <!--多选框-->
      <el-table-column type="selection" width="55" reserve-selection>
      </el-table-column>
      <el-table-column type="index" width="70" label="序号" fixed="left">
      </el-table-column>
      <el-table-column prop="typeName" label="名称" width="290" fixed="left">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template scope="scope">
          <span v-if="scope.row.status==0"  style="color:#e64242">不可用</span>
          <span v-if="scope.row.status==1"  style="color:#13ce66">可用</span>
        </template>
      </el-table-column>

      <el-table-column prop="updateTime" label="修改时间" width="170" sortable>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right">
        <template scope="scope">
          <el-button size="small"  @click="edit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

		<!-- 分页的大学   工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" :disabled="this.sels.length===0" @click="delBatch">批量删除</el-button>
			<el-pagination
					@size-change="handleSizeChange"
					@current-change="handleCurrentChange"
					:current-page="currentPage"
					:page-sizes="[5, 10]"
					:page-size="pageSize"
          :total="totals"
					layout="total, sizes, prev, pager, next, jumper"
          background
					style="float:right;">
			</el-pagination>
		</el-col>

    <!--
      :model="saveForm"  - 表单绑定模型数据对象saveForm
      label-width="80px"  - 标签的宽度
      :rules="saveFormRules" - 验证规则 - 表单项校验
      ref="saveForm" - 为当前对话框取名saveForm - 做表单校验

      label="姓名" - 标签名字
      prop="name" - 要做校验的时候必须要，不做校验就没有必要
      v-model="saveForm.name" - 双向绑定
      auto-complete="off" - 自动完成 - 关闭
    -->
    <!--新增界面-->
    <el-dialog :title="title" :visible.sync="saveFormVisible"  :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="名称" prop="typeName">
          <el-input v-model="saveForm.typeName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <!--状态(0:不可用;1:可用)-->
          <el-radio-group v-model="saveForm.status">
            <el-radio :label="1" border>可用</el-radio>
            <el-radio :label="0" border>不可用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" >提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>

	export default {
		data() {
			return {
			  //1.分页的数据
        tableDatas:[], //当前页数据
        listLoading:false,
        currentPage:1,//当前页
        pageSize:5,//每页显示的条数
        totals:0,//总条数

        //2.高级查询
        keyword:'',

        //3.当前行的数据   取id
        row:"",

        //4.批量删除选择
				sels: [], //存储多选记录

        //新增修改的对话框
        saveFormVisible:false,
        title:'',
				saveForm:{
          id:'',
          typeName:'',
          status:1,//默认可用
          updateTime:'',
				},
        //表单项验证
        saveFormRules: {
          typeName: [
            {required: true, message: '请输入文章类型名称', trigger: 'blur'}
          ],
          status: [
            {required: true, message: '请选择状态', trigger: 'blur'}
          ]
        },

			}
		},
		methods: {
      getKey(row){//跨页删除
        return row.id;},
      //高级查询----第几页时触发
      keywordQuery() {
        this.currentPage=1;//重置第一页
        this.getTableData();
      },
      //选择第几页时触发
      handleCurrentChange(curentPage) {
        this.currentPage = curentPage;
        this.getTableData();
      },
      //选择每页显示记录条数
      handleSizeChange(pageSize) {
        this.pageSize = pageSize;
        this.getTableData();
      },

    //----------------------------------------------
      //1.获取列表数据
      getTableData(){
        let para = {
          "currentPage": this.currentPage,
          "pageSize": this.pageSize,
          "keyword":this.keyword
        };
        this.listLoading = true; //显示加载圈
        //分页查询
        this.$http.post("/articleType", para).then(result=>{
          this.totals = result.data.total;//总条数
          this.tableDatas = result.data.list;//模型数据赋值
          this.listLoading = false;  //关闭加载圈
        });
      },

      //2.删除单条数据
      del(row){

        this.$confirm('确定删除此条数据吗？', '请确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.delete("/articleType/" + row.id).then(result=>{
            if(result.data.success){
              this.$message.success("删除成功");//成功
              //---------------------------------------
              //删除成功之后对当前页的处理【特殊情况：删除最后一页的最后一条数据】
              var totalPage = Math.ceil(this.totals/this.pageSize); //总页数 = 最后一页
              //如果当前页是最后一页                  并且是最后一条数据                  并且不是第一页
              if(this.currentPage==totalPage && (this.totals-1)%this.pageSize==0 && this.currentPage!=1){
                this.currentPage--;
              }

              this.getTableData();
            }else{
              this.$message.error("系统繁忙【500】");
            }
          }).catch(res=>{
            this.$message.error("系统繁忙【400,404】");
          });
        }).catch(() => {
          this.$message.info("已取消操作")
        });
      },

      //3.表格多条数据选中事件
      handleSelectionChange(val) {
        /**
         * val中存放的是所有已选择的行数据集合
         */
        this.sels = val.map(res => res.id);
        console.log(this.sels)
      },


      //3.批量删除
      delBatch(){
        this.$confirm('确定删除这些数据吗？', '请确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.patch("/articleType", this.sels).then(result=>{
            if(result.data.success){
              //答应后端的成功信息
              this.$message.success(result.data.message);//成功
              this.currentPage=1;//重置第一页
              this.getTableData();
            }else{
              this.$message.error("系统繁忙【500】");
            }
          }).catch(res=>{
            this.$message.error("系统繁忙【400,404】");
          });
        }).catch(() => {
          this.$message.info("已取消操作")
        });
      },

      //4.添加数据
      addHandler(){
        this.title="新增";
        this.saveForm = {
          id:'',
          typeName:'',
          status:1,//默认可用
          updateTime:'',
        };
        this.saveFormVisible = true;//显示对话框
      },
      //4.点击编辑 ---修改
      edit(row){
        this.title="修改";
        this.saveFormVisible = true;//显示编辑框
        this.saveForm = Object.assign({}, row);//回显数据
      },

			//6.提交新增表单数据
      saveSubmit(){
        this.$http.put("/articleType", this.saveForm).then(result=>{
          this.saveFormVisible = false;
					if(result.data.success){
            //答应后端的成功信息
            this.$message.success(result.data.message);//成功
            this.getTableData();
          }else{
            //this.$message.error("系统繁忙【500】");
            //打印后端的错误信息
            this.$message.error(result.data.message);
          }
				}).catch(res=>{
          this.$message.error("系统繁忙【400,404】");
        });
			},


      handleChange(value) {
        console.log(this.saveForm.courseTypeId);
      },







		},
		mounted() {
		    this.getTableData();
		}
	}

</script>

<style scoped>

</style>
